package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Convert;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.Collections;
import com.stlm2.dems.dao.CollectionsDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("collectionsService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class CollectionsService extends BaseServiceImpl<Collections> {

	@Resource
	private CollectionsDao collectionsDao;

	@Resource
	private DdmGoodsDao ddmGoodsDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Collections> getDao() {
		return collectionsDao;
	}


	/**
	 * 添加收藏
	 * ffl
	 * 2017-10-16 13:36:53
	 * @param token
	 * @param goodsId
	 * @return
	 */
	public  String addCollections (String token,String goodsId){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(goodsId)){
			return CodeContent.getModelByCode(0,"请添加收藏商品",null);
		}
		ModelMap model=new ModelMap();
		List<Collections> old=collectionsDao.getByGoodsId(user.getId(),NumberUtils.toInt(goodsId));
		if (old!=null&&old.size()>0){//该用户已添加过该商品
			model.put("collectionsId",old.get(0).getId());
			return CodeContent.getModelByCode(1,"",model);
		}

		Collections collections=new Collections();
		collections.setUserId(user.getId());
		collections.setGoodsId(NumberUtils.toInt(goodsId));
		collections.setCreateTime(new Date());
		collectionsDao.add(collections);
		model.put("collectionsId",collections.getId());
		return CodeContent.getModelByCode(1,"",model);
	}

	/**
	 * 我的收藏
	 * ffl
	 * 2017-10-16 13:51:56
	 * @param token
	 * @param pageNu
	 * @return
	 */
	public String myCollections(String token,String pageNu){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		int pageNo=1;
		if (NumberUtils.isDigits(pageNu)){
			pageNo=NumberUtils.toInt(pageNu);
		}else {
			return CodeContent.getModelByCode(0,"指定查询页数",null);
		}
		Collections param=new Collections();
		param.setUserId(user.getId());
		PageView pageView=collectionsDao.queryPageView(pageNo,10,param);
		ModelMap model=new ModelMap();
		if (pageView!=null){
			model.put("collectionsList",convert(pageView.getRecords()));
			model.put("recordsTotal", pageView.getRowCount());
			model.put("totalPage", pageView.getPageCount());
		}
		model.put("pageNo", pageNo);
		return CodeContent.getModelByCode(1,"",model);
	}

	public List<ModelMap> convert(List<Collections> collectionsList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (collectionsList != null && collectionsList.size() > 0) {
			for (Collections collections : collectionsList) {
				ModelMap map = buildModel(collections);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(Collections collections) {
		ModelMap model = new ModelMap();
		if(collections!=null) {
			DdmGoods goods=ddmGoodsDao.getById(collections.getGoodsId());
			if (goods!=null) {
				model.put("id", collections.getId());
				model.put("goodsId", goods.getId());
				model.put("name", goods.getName());
				model.put("salesPrice", goods.getMemberPrice());
				model.put("goodsState", goods.getState());
				model.put("mainImage", goods.getMainImg());
			}
		}
		return  model;
	}

	/**
	 * 删除我的收藏
	 * ffl
	 * 2017-10-16 14:24:33
	 * @param token
	 * @param idStr
	 * @return
	 */
	public String deleteById(String token,String idStr){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(StringUtils.isNullOrEmpty(idStr)){
			return CodeContent.getModelByCode(0,"请指定要删除的商品收藏",null);
		}
		String[] id=idStr.split(":");
		for (int i=0;i<id.length;i++){
			Collections collections=collectionsDao.getById(NumberUtils.toInt(id[i]));
			if(collections!=null){
				if(collections.getUserId()-user.getId()==0){
					collectionsDao.delete(collections.getId());
				}else {
					return CodeContent.getModelByCode(0,"要删除的收藏不是你的收藏",null);
				}
			}
		}

		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 清空我的收藏
	 * ffl
	 * 2017-10-16 14:35:25
	 * @param token
	 * @return
	 */
	public  String cleanCollection(String token){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		collectionsDao.cleanMyCollections(user.getId());
		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 我的收藏 个数
	 * ffl
	 * 2017-10-16 17:19:44
	 * @param token
	 * @return
	 */
	public  String myCollectionNum(String token){
		User user=userDao.getByToken(token);
		if (user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		Integer num=collectionsDao.getCount(user.getId());
		ModelMap model=new ModelMap();
		model.put("num",num);
		return CodeContent.getModelByCode(1,"",model);
	}
}